package com.sevenre.trackre.driver;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.sevenre.trackre.driver.datatypes.Trip;
import com.sevenre.trackre.driver.datatypes.TripAdapter;
import com.sevenre.trackre.driver.network.Server;
import com.sevenre.trackre.driver.utils.StartTripDialogBox;

public class ActivityPickUpList extends ListActivity {

	ListView lv;
	ArrayList<Trip> result;
	ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb(243,187,90)));
		getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.icon_action_bar_select_trip));
		getListView().setBackgroundResource(R.drawable.background_select_trip);
		
		dialog = new ProgressDialog(ActivityPickUpList.this);
		dialog.setTitle("Getting trips");
		dialog.setMessage("Trip list is being fetched, please wait\nIf it takes longer time then please cancel and try again");
		dialog.setButton("Cancel", new android.content.DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		});
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		new GetPickUpTrip().execute();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		final Intent i = new Intent(ActivityPickUpList.this, ActivityTagging.class);
		String msg = "Do you want to start trip - " + ((Trip)result.get(position)).getname() + " ?";
		StartTripDialogBox d = new StartTripDialogBox(ActivityPickUpList.this, msg, i);
		d.show();
		super.onListItemClick(l, v, position, id);
	}

	public void updateListView(ArrayList<Trip> results) {
		setListAdapter(new TripAdapter(this, results,
				R.layout.list_item_pick_up));
		ListView lv = (ListView) findViewById(android.R.id.list);
		lv.setPadding(10, 10, 10, 10);
		// int[] colors = { 0, 0, 0 }; // red for the example
		// lv.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
		lv.setDividerHeight(3);
		lv.setBackgroundResource(R.drawable.background_select_trip);
		dialog.cancel();
	}

	class GetPickUpTrip extends AsyncTask<String, Integer, ArrayList<Trip>> {

		@Override
		protected ArrayList<Trip> doInBackground(String... arg0) {
			return Server.getPickUpTrip();
		}

		@Override
		protected void onPostExecute(ArrayList<Trip> result) {
			updateListView(result);
			ActivityPickUpList.this.result = result;
			super.onPostExecute(result);
		}
	}
}
